//JavaScript Framework 2.0 Code
try{
Type.registerNamespace('com.yonyou.ncmm_pac_pda.TransInListController');
com.yonyou.ncmm_pac_pda.TransInListController = function() {
    com.yonyou.ncmm_pac_pda.TransInListController.initializeBase(this);
    this.initialize();
}
function com$yonyou$ncmm_pac_pda$TransInListController$initialize(){
    //you can programing by $ctx API
    //get the context data through $ctx.get()
    //set the context data through $ctx.push(json)
    //set the field of the context through $ctx.put(fieldName, fieldValue)
    //get the parameter of the context through $ctx.param(parameterName)
    //Demo Code:
    //    var str = $ctx.getString();      //获取当前Context对应的字符串
    //    alert($ctx.getString())          //alert当前Context对应的字符串
    //    var json = $ctx.getJSONObject(); //获取当前Context，返回值为json
    //    json["x"] = "a";        //为当前json增加字段
    //    json["y"] = [];           //为当前json增加数组
    //    $ctx.push(json);            //设置context，并自动调用数据绑定
    //    
    //    put方法需手动调用databind()
    //    var x = $ctx.get("x");    //获取x字段值
    //    $ctx.put("x", "b");     //设置x字段值
    //    $ctx.put("x", "b");     //设置x字段值
    //    $ctx.databind();            //调用数据绑定才能将修改的字段绑定到控件上
    //    var p1 = $param.getString("p1");   //获取参数p2的值，返回一个字符串
    //    var p2 = $param.getJSONObject("p2");   //获取参数p3的值，返回一个JSON对象
    //    var p3 = $param.getJSONArray("p3");   //获取参数p1的值，返回一个数组
    
    //your initialize code below...
    
}
    
function com$yonyou$ncmm_pac_pda$TransInListController$evaljs(js){
    eval(js)
}
//今天
function com$yonyou$ncmm_pac_pda$TransInListController$getDataToday(sender, args){
    var daydate = NCMMDateUtil.getDayDate();
    var dbilldate = "between '" + daydate.beginDate + "' and '" + daydate.endDate + "'";
    //加载当天数据
    var condition = {
    	"dbilldate": dbilldate
    };
    downloadBillData(condition);
}
//本周
function com$yonyou$ncmm_pac_pda$TransInListController$getDataWeek(sender, args){
    var daydate = NCMMDateUtil.getWeekDate();
    var dbilldate = "between '" + daydate.beginDate + "' and '" + daydate.endDate + "'";
    //加载当天数据
    var condition = {
    	"dbilldate": dbilldate
    };
    downloadBillData(condition);
}
//本月
function com$yonyou$ncmm_pac_pda$TransInListController$getDataMonth(sender, args){
    var daydate = NCMMDateUtil.getMonthDate();
    var dbilldate = "between '" + daydate.beginDate + "' and '" + daydate.endDate + "'";
    //加载当天数据
    var condition = {
    	"dbilldate": dbilldate
    };
    downloadBillData(condition);
}
//下载调拨出库单
function downloadBillData(condition) {
    //获取默认组织，如果默认组织为空，提示
    var stororg = $stringToJSON($cache.read("defaultstockorg"));
    if(stororg == null || stororg == "") {
    	$toast("下载前请先进行业务设置！");
    	return;
    }
    //调入组织
    condition["cothercalbodyoid"] = "='" + stororg.pk_stockorg + "'";
	//调入仓库
	var stordoc = $stringToJSON($cache.read("defaultstordoc"));
		if(stordoc != null && stordoc != "") {
		condition["cotherwhid"] ="in('"+stordoc.pk_stordoc+"','~')";
		}
	$js.showLoadingBar();
	//测试连接
	NCMMDBUtil.testNCConnection(function(data1) {
	    if (data1 == false || data1 == "false") {
	        $js.hideLoadingBar();
	        $toast("下载失败请检查网络设置！");
	        return;
	    } else {
	        //下载调拨出库单VO数据
	        NCMMDBUtil.loadNCBaseData(TransInVO, TransInVO, NCMMHttpUtil.BILL, condition, function(data) {
	            if(data != null) {
	                //获取本地旧数据，没有扫描数量清空
	                var vocount = NCMMDBUtil.getBillCount(data);
	                var billinfos = data.billinfos;	
	                var insertvos=[];
	                var updatevos=[];
	                if(NCMMPubUtil.isNotEmpty(billinfos)&&billinfos.length > 0) {
	                    var oldData = getOldData();
	                    for(var i = 0; i < billinfos.length; i++) {
	                        //插入表
	                        var items = billinfos[i].billinfo.items;
	                        for(var j = 0; j < items.length; j++) {
	                            var billdata = $.extend({}, items[j].item, billinfos[i].billinfo.head);
	                            if(!NCMMPubUtil.isEmpty(oldData) && !NCMMPubUtil.isEmpty(oldData[billdata.cgeneralbid])) {
	                                billdata["nscanednum"] = oldData[billdata.cgeneralbid].nscanednum;
	                                billdata["nscanedastnum"] = oldData[billdata.cgeneralbid].nscanedastnum;	
	                                updatevos.push(billdata);
	                                //updateScanTable(billdata);
	                            } else {
	                                insertvos.push(billdata);
	                            }
	                        }
	                    }
	                    if(NCMMPubUtil.isNotEmpty(insertvos)){
	                        TransInUtil.batchInsertTableVOs(TransInVO,insertvos);
	                    }
	                    if(NCMMPubUtil.isNotEmpty(updatevos)){
	                        TransInUtil.updateBatchVOs(TransInVO,updatevos,TransInVO.NSCANEDNUM.name);
	                    }
	                    loadTransInListData();
	                    $js.hideLoadingBar();
	                    $toast("调拨出库下载了【" + vocount + "】条数据。");
	                } else {
	                    $js.hideLoadingBar();
	                    $toast("没有获取到符合条件的订单！");
	                }
	            } else {
					$js.hideLoadingBar();
					$toast("没有获取到符合条件的订单！");
				}
	        });
	    }
	});
}
    //获取本地数据，如果没有扫描数量，直接删除，有，返回json格式【行id，vo】
	function getOldData() {
		var returnData = {};
		NCMMPubUtil.queryVOByConditions(TransInVO, null, function(data) {
			if(data != null) {
				var datas = eval(data);
				for(var i = 0; i < datas.length; i++) {				
					if(NCMMPubUtil.isNotEmpty(datas[i].nscanednum)&&parseFloat(datas[i].nscanednum)>0) {
						returnData[datas[i].cgeneralbid] = datas[i];
					} else {
						//删除
						NCMMPubUtil.deleteVO(TransInVO, datas[i]);
					}
				}
			}
		});
		return returnData;
	}
//加载材料出库单数据
function loadTransInListData() {
    NCMMPubUtil.queryVOByConditions(TransInVO, null, function(data) {
        var showData = eval(data);
        showData = NCMMPubUtil.htmlspecialjsonarray2UI(showData);
        var bitnumberJson = {};
        for(var i = 0; i < showData.length; i++) {
            var itemData = showData[i];
            if(NCMMPubUtil.isEmpty(bitnumberJson) || NCMMPubUtil.isEmpty(bitnumberJson[itemData["cunitid"]])) {
                var bitnumber = TransInUtil.getUnitBitnumber(itemData["cunitid"]);
                if(!NCMMPubUtil.isEmpty(bitnumber)) {
                    bitnumberJson[itemData["cunitid"]] = bitnumber;
                }
            }
            if(NCMMPubUtil.isEmpty(bitnumberJson) || NCMMPubUtil.isEmpty(bitnumberJson[itemData["castunitid"]])) {
                var bitnumber = TransInUtil.getUnitBitnumber(itemData["castunitid"]);
                if(!NCMMPubUtil.isEmpty(bitnumber)) {
                    bitnumberJson[itemData["cunitid"]] = bitnumber;
                }
            }
            if(!NCMMPubUtil.isEmpty(bitnumberJson) && !NCMMPubUtil.isEmpty(bitnumberJson[itemData["cunitid"]])) {
                showData[i]["nshouldinnum"] = parseFloat(showData[i]["nshouldinnum"]).toFixed(bitnumberJson[itemData["cunitid"]]);
                if(!NCMMPubUtil.isEmpty(showData[i]["nscanednum"])){
                    showData[i]["nscanednum"] = parseFloat(showData[i]["nscanednum"]).toFixed(bitnumberJson[itemData["cunitid"]]);
                }
            }
            if(!NCMMPubUtil.isEmpty(bitnumberJson) && !NCMMPubUtil.isEmpty(bitnumberJson[itemData["castunitid"]])) {
                showData[i]["nshouldassistnum"] = parseFloat(showData[i]["nshouldassistnum"]).toFixed(bitnumberJson[itemData["castunitid"]]);
                if(!NCMMPubUtil.isEmpty(showData[i]["nscanedastnum"])){
                    showData[i]["nscanedastnum"] = parseFloat(showData[i]["nscanedastnum"]).toFixed(bitnumberJson[itemData["castunitid"]]);
                }
            }
        }
        var jsondata = {
            listdata: showData
        }
        $ctx.push(jsondata);
        $ctx.dataBind();
    });
    //NCMMPubUtil.notifyFilter("search0", "listviewdefine0", ["vbillcode", "cinventoryvid_name", "nnum", "nastnum", "nscanednum"]);
}
//返回按钮
function com$yonyou$ncmm_pac_pda$TransInListController$close(sender, args){
    $view.close();
}
//加载数据
function com$yonyou$ncmm_pac_pda$TransInListController$onload(sender, args){
    NCMMPubUtil.createTable(TransInVO);
    loadTransInListData();
}
function com$yonyou$ncmm_pac_pda$TransInListController$showMenu(sender, args){
    TransInUtil.openDropDownList("btn_menu");
}
//查询按钮
function com$yonyou$ncmm_pac_pda$TransInListController$openSearchView(sender, args){
    $view.open({
        viewid: "com.yonyou.ncmm_pac_pda.TransInQuery",
        isKeep: "true",
        callback: "doQuery()"
    });
}
function doQuery() {
    var cdon = $param.getJSONObject("qcondition");
    var json = {};
    json = cdon;
    downloadBillData(json);
}
//单据码扫描
function com$yonyou$ncmm_pac_pda$TransInListController$scanBillCode(sender, args){
    $scanner.open({
        bindfield: "billbarcode",
        callback: "billScanBack()"
    });
}
//手工录入条码或者pda机红外扫码
function scanonchange(sender, args){
    billScanBack();
    $ctx.put("billbarcode","");
    $ctx.dataBind();
}
//单据码解析
function billScanBack() {
    //获取条码值
    var billbarcode = $ctx.getString("billbarcode");
    var condition = {
        "vbillcode": billbarcode
    }
    NCMMPubUtil.queryVOByConditions(TransInVO, condition, function(data) {
        if(typeof(data) == 'undefined' || data == null || data.length == 0) {
            $toast("未找到单据码信息，请重新下载单据信息");
            return;
        }
        //打开扫描记录编辑界面
        var vos = eval(data);
        vos = NCMMPubUtil.htmlspecialjsonarray2UI(vos);
        if(vos != null && vos.length > 0&&NCMMPubUtil.isNotEmpty(vos[0]["vbillcode"])) {
            var billinfo = {};
            var billcode = vos[0]["vbillcode"];
            billinfo = {
                "vbillcode": billcode,
                "cgeneralhid": vos[0]["cgeneralhid"]
            };
            $view.open({
                viewid: "com.yonyou.ncmm_pac_pda.TransInScan",
                isKeep: "true",
                row: billinfo
            });
        } else {
            $toast("未找到单据码信息，请重新下载单据信息");
        }
    });
}
//行操作按钮
function com$yonyou$ncmm_pac_pda$TransInListController$showItemButton(sender, args){
    $view.openPop(popDialogInfo);
}
var popDialogInfo = {
    "viewid" : "panel_btn",
    "popgravity" : "popbottom"
}
//详情
function com$yonyou$ncmm_pac_pda$TransInListController$showDetail(sender, args) {
    $view.closePop(popDialogInfo);
    var row = $id("listviewdefine0").get("row");
    $view.open({
        "viewid": "com.yonyou.ncmm_pac_pda.TransInDetail",
        "isKeep": "true",
        "row": row
    });
}
//采集
function com$yonyou$ncmm_pac_pda$TransInListController$doScan(sender, args) {
    $view.closePop(popDialogInfo);
    var row = $id("listviewdefine0").get("row");
    $view.open({
        "viewid": "com.yonyou.ncmm_pac_pda.TransInScan",
        "isKeep": "true",
        "row": row
    });
}
//删除
function com$yonyou$ncmm_pac_pda$TransInListController$itemDelete(sender, args) {
    $view.closePop(popDialogInfo);
    var params = {
        "title": "提示",
        "message": "确定删除所选记录吗？",
        "okbuttontitle": "确定",
        "cancelbuttontitle": "取消",
        "style": "ok-cancel",
        "okaction": "doDelete()",
        "cancelaction": "doCancel()"
    }
    $view.openDialog(params);
}
//删除按钮取消
function doCancel() {
    $toast("删除操作已取消。");
}
//删除按钮确定
function doDelete() {
    var row = $id("listviewdefine0").get("row");
    row = $stringToJSON(row);
    if(row.nscanednum != 0 && row.nscanednum != "" && row.nscanednum != null) {
        $alert("已扫描数量不为0，不能删除！");
        return;
    }
    NCMMPubUtil.deleteVO(TransInVO, row);
    $toast("删除成功。");
    loadTransInListData();
}
//删除
function com$yonyou$ncmm_pac_pda$TransInListController$itemCancel(sender, args) {
    $view.closePop(popDialogInfo);
}

com.yonyou.ncmm_pac_pda.TransInListController.prototype = {
    onrestart : com$yonyou$ncmm_pac_pda$TransInListController$onload,
    showDetail : com$yonyou$ncmm_pac_pda$TransInListController$showDetail,
    doScan : com$yonyou$ncmm_pac_pda$TransInListController$doScan,
    itemDelete : com$yonyou$ncmm_pac_pda$TransInListController$itemDelete,
    itemCancel : com$yonyou$ncmm_pac_pda$TransInListController$itemCancel,
    showItemButton : com$yonyou$ncmm_pac_pda$TransInListController$showItemButton,
    scanBillCode : com$yonyou$ncmm_pac_pda$TransInListController$scanBillCode,
    openSearchView : com$yonyou$ncmm_pac_pda$TransInListController$openSearchView,
    showMenu : com$yonyou$ncmm_pac_pda$TransInListController$showMenu,
    onload : com$yonyou$ncmm_pac_pda$TransInListController$onload,
    close : com$yonyou$ncmm_pac_pda$TransInListController$close,
    getDataMonth : com$yonyou$ncmm_pac_pda$TransInListController$getDataMonth,
    getDataWeek : com$yonyou$ncmm_pac_pda$TransInListController$getDataWeek,
    getDataToday : com$yonyou$ncmm_pac_pda$TransInListController$getDataToday,
    initialize : com$yonyou$ncmm_pac_pda$TransInListController$initialize,
    evaljs : com$yonyou$ncmm_pac_pda$TransInListController$evaljs
};
com.yonyou.ncmm_pac_pda.TransInListController.registerClass('com.yonyou.ncmm_pac_pda.TransInListController',UMP.UI.Mvc.Controller);
}catch(e){
    $e(e);
}
